<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>目录列表</title>
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">目录ID</label>
                            <div class="layui-input-inline">
                                <input id="listId" type="text" name="listId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">目录名称</label>
                            <div class="layui-input-inline">
                                <input id="listName" type="text" name="listName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">课程名称</label>
                            <div class="layui-input-inline">
                                <input id="courseName" type="text" name="courseName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="search"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal data-add-btn" lay-event="add">添加目录</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <div id="pageDiv"></div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="edit">编辑信息</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除目录</a>
        </script>
        <script type="text/html" id="lockState">
            {{# if(d.lockState){}}
            <span>VIP观看</span>
            {{#}else{}}
            <span>免费观看</span>
            {{#}}}
        </script>
        <script type="text/html" id="timeLength">
            {{# if(d.timeSecond<10){}}
                {{# if(d.timeMinute<10){}}
                    <span>&nbsp;&nbsp;{{d.timeMinute}}</span> 分 0<span>{{d.timeSecond}} 秒</span>
                {{#}else{}}
                    <span>{{d.timeMinute}}</span> 分 0<span>{{d.timeSecond}} 秒</span>
                {{#}}}
            {{#}else{}}
                {{# if(d.timeMinute<10){}}
                    <span>&nbsp;&nbsp;{{d.timeMinute}}</span> 分 <span>{{d.timeSecond}} 秒</span>
                {{#}else{}}
                    <span>{{d.timeMinute}}</span> 分 <span>{{d.timeSecond}} 秒</span>
                {{#}}}
            {{#}}}
        </script>
    </div>
</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="none">
    let myTable;
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        myTable=table.render({
            elem: '#currentTableId',
            url: '/directory/queryAllDirectory',
            method:"post",
            toolbar: '#toolbarDemo',
            parseData:function (res){
                return{
                    "code":0,
                    "msg":res.message,
                    "count":res.data.total,
                    "data":res.data.list
                }
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'listId', width: "8%", title: '目录编号', sort: true, align: "center"},
                {field: 'listName', width: "22%", title: '目录名称', align: "center"},
                {field: 'courseId', width: "10%", title: '课程ID', hide:true, align: "center"},
                {field: 'courseName', width: "22%", title: '所属课程', sort:true, align: "center"},
                {field: 'videoUrl', title: '视频路径', hide: true, align: "center"},
                {width: "12%", title: '视频时长',templet:'#timeLength', align: "center"},
                {field: 'lockState', width: "15%", title: 'VIP状态', sort: true,templet: '#lockState', align: "center"},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                ,curr: 1 //设定初始在第 5 页
                ,limit: 10
                ,limits: [10,15,30]
                ,groups: 5 //只显示 1 个连续页码
                ,first: true //不显示首页
                ,last: true //不显示尾页
            },
            request:{
                pageName:"pageNum",
                limitName: "pageSize"
            },
            done:function (){
                $("table").css("width","100%");
            }
        });

        // 监听搜索操作
        form.on('submit(search)', function (data) {
            console.log("搜索");
            myTable.reload({
                url:"/directory/selCourseDirectory",
                method: "post",
                page: {
                    curr: 1 //设定初始在第 5 页
                    ,limit: 10
                },
                request:{
                    pageName:"pageNum",
                    limitName: "pageSize"
                },
                where:{
                    listId:data.field.listId,
                    listName:data.field.listName,
                    courseName:data.field.courseName
                },
            });
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let index = layer.open({
                    title: '添加目录',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/directory/goToCourseList?listId=0'
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                $.ajax({
                    type: "post",
                    url: '/directory/isEdit',
                    data: {courseId: data.courseId},
                    success: function (res) {
                        if (res.code === 200) {
                            let index = layer.open({
                                title: '编辑课程目录',
                                type: 2,
                                shade: 0.2,
                                maxmin:true,
                                shadeClose: true,
                                area: ['100%', '100%'],
                                content: '/directory/goToCourseList?listId='+data.listId
                            });
                            $(window).on("resize", function () {
                                layer.full(index);
                            });
                            return false;
                        } else {
                            layer.msg(res.message, {time: 5000, icon: 1, offset: [15]});
                        }
                    },
                    error: function (error) {
                        layer.msg(error, {time: 5000, icon: 2, offset: [15]})
                    }
                })
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除《'+data.listName+'》目录信息吗？',{icon:3}, function (index) {
                    $.ajax({
                        type: "get",
                        url: '/directory/delDirectory',
                        data: {listId: data.listId},
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.message, {time: 5000, icon: 1, offset: [15]});

                            } else {
                                layer.msg(res.message, {time: 5000, icon: 1, offset: [15]});
                            }
                        },
                        error: function (error) {
                            layer.msg(error, {time: 5000, icon: 2, offset: [15]})
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>
</body>
</html>